package com.cskaoyan.catmall.controller.background;

import com.cskaoyan.catmall.model.bo.BatchDeleteTopicIdsBo;
import com.cskaoyan.catmall.model.bo.PromotionTopicBo;
import com.cskaoyan.catmall.model.pojo.MarketTopic;
import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.model.vo.ItemOfPromotionTopicVo;
import com.cskaoyan.catmall.model.vo.ItemOfTopicInfoVo;
import com.cskaoyan.catmall.model.vo.PromotionBaseVo;
import com.cskaoyan.catmall.service.TopicService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 创建日期: 2022-01-09-20-13
 * author : LL
 * description: 有关专题管理的请求
 */
@RequestMapping("admin/topic")
@RestController
public class PromotionTopicController {
    
    @Autowired
    TopicService topicService;
    
    

    @PostMapping("batch-delete")
    public BaseRespVo batchDeleteTopic(@RequestBody BatchDeleteTopicIdsBo topicIdsBo) {
        topicService.batchDeleteTopicById(topicIdsBo);
        return BaseRespVo.ok();
    }
    /**
     * @param 推广管理参数，除基本包含页码及排序的参数外，还包含专题标题及子标题
     * @return 推广管理返回值，其中列表为专题管理列表
     * @description 推广管理中专题管理
     * @author LL
     * @date 2022/1/7
     */
    @RequiresPermissions("admin:topic:list")
    @GetMapping("list")
    public BaseRespVo getPromotionTopic(PromotionTopicBo promotionTopicBo) {
        PageHelper.startPage(promotionTopicBo.getPage(), promotionTopicBo.getLimit());
        PromotionBaseVo<ItemOfPromotionTopicVo> promotionBaseVo = topicService.getPromotionTopic(promotionTopicBo);
        PageInfo<ItemOfPromotionTopicVo> pageInfo = new PageInfo<>(promotionBaseVo.getList());
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        promotionBaseVo.setPages(pages);
        promotionBaseVo.setTotal((int) total);
        promotionBaseVo.setLimit(promotionTopicBo.getLimit());
        promotionBaseVo.setPage(promotionTopicBo.getPage());
        return BaseRespVo.ok(promotionBaseVo);

    }

    /**
     * @param
     * @return
     * @description 创建专题
     * @author LL
     * @date 2022/1/8
     */
    @RequiresPermissions("admin:topic:create")
    @PostMapping("create")
    public BaseRespVo createTopic(@RequestBody MarketTopic marketTopic) {
        topicService.createTopic(marketTopic);
        return BaseRespVo.ok(marketTopic);
    }

    /**
     * @param
     * @return
     * @description 展示专题具体内容及相关商品内容
     * @author LL
     * @date 2022/1/8
     */
    @RequiresPermissions("admin:topic:read")
    @GetMapping("read")
    public BaseRespVo getTopicInfo(Integer id) throws JsonProcessingException {
        ItemOfTopicInfoVo topicInfoVo = topicService.getTopicInfo(id);
        return BaseRespVo.ok(topicInfoVo);
    }

    /**
     * @param
     * @return
     * @description 更新专题管理相关内容
     * @author LL
     * @date 2022/1/8
     */
    @RequiresPermissions("admin:topic:read")
    @PostMapping("update")
    public BaseRespVo updateTopic(@RequestBody MarketTopic marketTopic) {
        topicService.updateTopic(marketTopic);
        return BaseRespVo.ok(marketTopic);
    }

    @RequiresPermissions("admin:topic:delete")
    @PostMapping("delete")
    public BaseRespVo deleteTopic(@RequestBody MarketTopic marketTopic) {
        topicService.deleteTopic(marketTopic);
        return BaseRespVo.ok();
    }

}